<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/
	
require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestLoginManager extends PHPUnit_Framework_TestCase {
	
	function test_login_with_already_logged_in() {
		$_SESSION['user_id'] = 1234;

		$manager = new LoginManager();

		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->never())
				  ->method('db_query_select');
		$db_access->expects($this->never())
				  ->method('db_do_query');
		$db_access->expects($this->never())
				  ->method('db_fetch_array');

		$manager->set_db_accessor($db_access);

		$manager->login();
	}

	function test_login_with_just_registered() {
		$expected_user_id = 7589;
		$expected_member = array('pref_logon_time' => 0);
		$expected_select_member_query = 'select_member_query';
		$expected_select_member_result = 'select_member_result';
		$expected_select_usergroup_query = 'select_usergroup_query';
		$expected_select_usergroup_result = 'select_usergroup_result';
		$expected_update_member_query = 'update_member_query';
		$expected_select_region_query = 'expected_select_region_query';
		$expected_select_region_result = 'expected_select_region_result';

		$_SESSION['user_id'] = null;

		$manager = new LoginManager();

		$mock_session_manager = $this->getMock('SessionManager');
		$mock_session_manager->expects($this->once())
				->method('session_register');
		$manager->set_session_manager($mock_session_manager);

		$mock_db_access = $this->getMock('DbAccessor');
		
		$mock_db_access->expects($this->at(0))
				->method('db_query_select')
				->with(array(array('*')), array("MEMBER"), "MEMBER.user_id = $expected_user_id")
				->will($this->returnValue($expected_select_member_query));
		$mock_db_access->expects($this->at(1))
				->method('db_do_query')
				->with($expected_select_member_query)
				->will($this->returnValue($expected_select_member_result));
		$mock_db_access->expects($this->at(2))
				->method('db_fetch_array')
				->with($expected_select_member_result)
				->will($this->returnValue($expected_member));
		$mock_db_access->expects($this->at(3))
				->method('db_query_select')
				->with(array(array("usergroup_id")), array("IN_USERGROUP"), "user_id = $expected_user_id")
				->will($this->returnValue($expected_select_usergroup_query));
		$mock_db_access->expects($this->at(4))
				->method('db_do_query')
				->with($expected_select_usergroup_query)
				->will($this->returnValue($expected_select_usergroup_result));
		$mock_db_access->expects($this->at(5))
				->method('db_fetch_array')
				->with($expected_select_usergroup_result)
				->will($this->returnValue(null));
		$mock_db_access->expects($this->at(6))
				->method('db_query_update')
				->with(array('member_since', 'last_visit'), array('member_since', 'NOW(14)'), 'MEMBER', "user_id = $expected_user_id")
				->will($this->returnValue($expected_update_member_query));
		$mock_db_access->expects($this->at(7))
				->method('db_do_query')
				->with($expected_update_member_query);
		$mock_db_access->expects($this->at(8))
				->method('db_query_select')
				->with(array(array('*')), array('IN_REGION'), "user_id = $expected_user_id")
				->will($this->returnValue($expected_select_region_query));
		$mock_db_access->expects($this->at(9))
				->method('db_do_query')
				->with($expected_select_region_query)
				->will($this->returnValue($expected_select_region_result));
		$mock_db_access->expects($this->at(10))
				->method('db_num_rows')
				->with($expected_select_region_result)
				->will($this->returnValue(0));

		$manager->set_db_accessor($mock_db_access);

		$manager->login($expected_user_id);

		$this->assertEquals($expected_user_id, $_SESSION['user_id']);
		$this->assertEquals($expected_member, $_SESSION['member']);
		$this->assertEquals(null, $_SESSION['user_ip_string']);
		$this->assertEquals(array(), $_SESSION['user_groups']);
	}
}

?>
